package com.example.nebulaspringboot.service.impl;

import com.example.nebulaspringboot.entity.Role;
import com.example.nebulaspringboot.entity.User;
import com.example.nebulaspringboot.error.BusinessException;
import com.example.nebulaspringboot.error.EmBusinessError;
import com.example.nebulaspringboot.mapper.RoleMapper;
import com.example.nebulaspringboot.mapper.UserMapper;
import com.example.nebulaspringboot.service.UserService;
import com.example.nebulaspringboot.service.model.UserModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public UserModel selectByPrimaryKey(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        Role role = roleMapper.selectById(user.getRoleId());
        UserModel userModel = this.convertUserModelFromDataObject(user,role);
        return userModel;
    }

    @Override
    public UserModel selectByUsername(String username){
        User user = userMapper.selectByUsername(username);
        System.out.println(user);
        if (user == null) {
            return null;
        }
        Role role = roleMapper.selectById(user.getRoleId());
        UserModel userModel = this.convertUserModelFromDataObject(user,role);
        return userModel;
    }

    @Override
    public List<UserModel> selectUserList() {
        List<User> userlList = userMapper.selectUserList();
        List<UserModel> userModelList = userlList.stream().map(user -> {
            Role role = roleMapper.selectById(user.getRoleId());
            UserModel userModel = this.convertUserModelFromDataObject(user,role);
            return userModel;
        }).collect(Collectors.toList());
        return userModelList;
    }

    @Override
    @Transactional
    public void insertSelective(User user) throws BusinessException {
        if(user.getTelphone() == null || user.getUsername() == null
            || user.getPassword() == null) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        UserModel userCheck = this.selectByUsername(user.getUsername());
        System.out.println(userCheck);
        if(userCheck != null) {
            throw new BusinessException(EmBusinessError.USER_IS_REPEAT);
        }
        userMapper.insertSelective(user);
        return ;
    }

    @Override
    @Transactional
    public void updateByIdSelective(User user) throws BusinessException {
//        throw new BusinessException(EmBusinessError.USER_IS_REPEAT);
        if(user.getTelphone() == null || user.getUsername() == null) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,"参数不合法");
        }
        UserModel userCheck = this.selectByUsername(user.getUsername());
        if (userCheck != null) {
            if (userCheck.getId() != user.getId()) {
                throw new BusinessException(EmBusinessError.USER_IS_REPEAT,"用户名重复");
            }
        }
        userMapper.updateByIdSelective(user);
        return ;
    }

    @Override
    @Transactional
    public void deleteById(Integer id) {
        userMapper.deleteById(id);
    }

    private UserModel convertUserModelFromDataObject(User user, Role role) {
        UserModel userModel = new UserModel();
        if(user == null) {
            return null;
        }
        BeanUtils.copyProperties(user,userModel);
        userModel.setRoleName(role.getName());
        return userModel;
    }
}
